using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Runtime;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    /// <summary>
    /// Deletes any roles assigned to a particular user on a list item
    /// </summary>
    public class DeleteListItemPermissionAssigment : Activity
    {
        #region Dependency Properties

        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (
                                                                                             DeleteListItemPermissionAssigment
                                                                                             ));


        public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof (string),
                                                                                      typeof (
                                                                                          DeleteListItemPermissionAssigment
                                                                                          ));

        public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof (int),
                                                                                        typeof (
                                                                                            DeleteListItemPermissionAssigment
                                                                                            ));

        public static DependencyProperty UserProperty = DependencyProperty.Register("User", typeof (string),
                                                                                    typeof (
                                                                                        DeleteListItemPermissionAssigment
                                                                                        ));

        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        [Description("ID of the list we are working with")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ListId
        {
            get { return ((string) (base.GetValue(ListIdProperty))); }
            set { base.SetValue(ListIdProperty, value); }
        }

        [Description("ID of the list item we are working with")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int ListItem
        {
            get { return ((int) (base.GetValue(ListItemProperty))); }
            set { base.SetValue(ListItemProperty, value); }
        }

        [Description("User from which we need to remove the permission")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string User
        {
            get { return ((string) (base.GetValue(UserProperty))); }
            set { base.SetValue(UserProperty, value); }
        }

        #endregion

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                PermissionRequest myRevokeRequest = new PermissionRequest();

                myRevokeRequest.RequestType = PermissionActionType.Revoke;
                myRevokeRequest.ItemId = ListItem;
                myRevokeRequest.ListID = new Guid(ListId);
                myRevokeRequest.SiteID = __Context.Site.ID;
                myRevokeRequest.WebID = __Context.Web.ID;
                myRevokeRequest.User = User;


                WorkflowEnvironment.WorkBatch.Add(PermissionsService.Instance, myRevokeRequest);


                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{

                //    using (SPSite site = new SPSite(__Context.Site.ID))
                //    {
                //        using (SPWeb web = site.AllWebs[__Context.Web.ID])
                //        {
                //            SPList list = web.Lists[new Guid(ListId)];

                //            SPListItem listItem = list.Items.GetItemById(ListItem);

                //            Common.RemoveListItemPermissionEntry(listItem, User, true);

                //            listItem.Update();

                //        }

                //    }

                //});
            }
            catch (Exception e)
            {
                Common.LogExceptionToWorkflowHistory(e, executionContext, WorkflowInstanceId);

                throw;
            }
            return ActivityExecutionStatus.Closed;
        }
    }
}